package io.renren.common.utils;

import cn.hutool.poi.excel.ExcelReader;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xiayiguo
 * @since 2022-02-12
 */
public class ExactStockFinder {
    public static void main(String[] args) throws IOException {
//        2022-02 金融 旅游 地产 基建 （新冠检测 新冠药尾期）总龙头 保利联合

        List<String> keys = Arrays.asList("锂电池");
        find("E:\\股市\\市场情绪数据库\\2020", keys);
    }

    public static void find(String parentDir, List<String> keys) throws IOException {
//        遍历文件夹下面所有文件
//        在所有单元格中匹配字符
//        匹配成功，非ST ，同时之前没有加入，加入返回的数组
        Set<Stock> stocks = new HashSet<>();
        List<List<Object>> lists = new ArrayList<List<Object>>();
        for (String key : keys) {
            Files.walk(Paths.get(parentDir)).forEach((path) -> {
                File bookFile = path.toFile();
                if (!bookFile.isDirectory()){
                    ExcelReader excelReader = new ExcelReader(bookFile, 0);
                    List<List<Object>> content = excelReader.read();
                    content.removeIf(objects -> doNotContainKeyWord(objects, key));
                    if (!content.isEmpty()) {
                        content.forEach(objects ->
                            lists.add(objects));
//                            stocks.add(new Stock((String)objects.get(0), (String)objects.get(1), key, String.valueOf(objects.get(2)), (String)objects.get(12))));
                    }
                }
            });
        }

        FileWriter fileWriter = new FileWriter("E:\\股市\\市场情绪数据库\\寻找标的\\"+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + System.currentTimeMillis() +  keys + ".txt");
        for (List<Object> stock : lists) {
            fileWriter.write(stock+"\n");
        }
        fileWriter.flush();
        fileWriter.close();

    }

    public static boolean doNotContainKeyWord(List<Object> row, String key) {
        for (Object cell : row) {
            if (cell.toString().contains(key)) {
                return false;
            }
        }
        return true;
    }
}
